function debounce(fn,delay=1000,immediate = false){
     let timer = null
     function newfn(...args){
      if(timer) clearTimeout(timer)
      if(immediate) {
        fn.apply(this,args)
        immediate = false
      }else{
        timer =  setTimeout(() => {
         fn.apply(this,args)
       }, delay);
      }
     }
     newfn.cancel = function(){ // 函数也是对象,所以在上面加一个方法
       clearTimeout(timer)
       timer = null
     }
     return newfn
    }
    export default debounce